package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.KafkaListeners;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 监听器
 */
@Component
@Slf4j
public class UpDownListener {


    @Autowired
    private IApArticleService apArticleService;

    @KafkaListener(topics = "${topic.upDownTopic}")
    public void handMsg(ConsumerRecord<String , String> record){
        String value = record.value();
        //1.判断数据是否存在
        if (StringUtils.isNotEmpty(value)){
            //2.获取消息内容
            log.info("【文章微服务】监听文章上下架：{}", value);
            HashMap hashMap = JSON.parseObject(value, HashMap.class);
            //2.1 获取内容
            Long articleId =(Long) hashMap.get("articleId");
            boolean isDown = (Boolean) hashMap.get("isDown");
            //3.执行文章微服务修改
            LambdaUpdateWrapper<ApArticle> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(ApArticle::getId,articleId);
            wrapper.set(ApArticle::getIsDown,isDown);
            apArticleService.update(wrapper);
        }

    }

}
